package Store;

import java.util.ArrayList;

public class Import {
	private String fileName = "";
	private Integer addedItems = 0;
	private Integer deletedItems = 0;
	private Integer saleStartedItems = 0;
	private Integer saleEndedItems = 0;
	private ItemListChange changeList = new ItemListChange();
	private String importID = "";
	private static Integer idGenerator = 0;
	
	public synchronized static String getUniqueImportID(){
		return ((Integer) idGenerator++).toString();
	}
	
	public Boolean parseData() throws Exception{
		ArrayList<Item> addItems = new ArrayList<Item>();
		ArrayList<Item> deleteItems = new ArrayList<Item>();
		ArrayList<Item> saleStartItems = new ArrayList<Item>();
		ArrayList<Item> saleEndItems = new ArrayList<Item>();
		
		//add code to parse the file
		
		this.addItems((Item[]) addItems.toArray());
		this.deleteItems((Item[]) deleteItems.toArray());
		this.saleItems((Item[]) saleStartItems.toArray());
		this.saleEndItems((Item[]) saleEndItems.toArray());
		return true;
	}
	
	public Import(String fileName){
		this.setFileName(fileName);	
		this.setImportID(getUniqueImportID());
	}
	
	public String getFileName() {
		return fileName;
	}
	
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	public void deleteItems(Item... items){
		for(Item item: items){
			deletedItems += 1;
			ItemChange temp = new ItemChange(item.getItemID(), 2);
			new DatabaseAccess().deleteItem(item);
			changeList.addToList(temp);
		}
	}
	
	public void addItems(Item... items) throws Exception{
		for(Item item: items){
			addedItems += 1;
			ItemChange temp = new ItemChange(item.getItemID(), 1);
			new DatabaseAccess().addItemsToDomain(item);
			changeList.addToList(temp);
		}
		
	}
	
	public void saleItems(Item... items) throws Exception{
		for(Item item: items){
			saleStartedItems += 1;
			ItemChange temp = new ItemChange(item.getItemID(), 3);
			new DatabaseAccess().changeItemToDomain(item);
			changeList.addToList(temp);
		}
		
	}
	
	public void saleEndItems(Item... items) throws Exception{
		for(Item item: items){
			saleEndedItems += 1;
			ItemChange temp = new ItemChange(item.getItemID(), 4);
			new DatabaseAccess().changeItemToDomain(item);
			changeList.addToList(temp);
		}
			
	}

	public void setImportID(String importID) {
		this.importID = importID;
	}

	public String getImportID() {
		return importID;
	}

	public Integer getAddedItems(){
		return addedItems;
	}
	
	public Integer getdeletedItems(){
		return deletedItems;
	}
	
	public Integer getSaleStartItems(){
		return saleStartedItems;
	}
	
	public Integer getSaleEndedItems(){
		return saleEndedItems;
	}

	public ItemListChange getChangeList(){
		return changeList;
	}
	
}
